package com.example;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;

public class CodeGenerator {
    public static void main(String[] args) {



        //代码生成器对象

        AutoGenerator autoGenerator = new AutoGenerator();
        //数据库相关配置
        DataSourceConfig dataSourceConfig = new DataSourceConfig();
        dataSourceConfig.setDriverName("com.mysql.cj.jdbc.Driver");
        dataSourceConfig.setUrl("jdbc:mysql://localhost:3306/tb");
        dataSourceConfig.setUsername("root");
        dataSourceConfig.setPassword("1234");
        autoGenerator.setDataSource(dataSourceConfig);
        //全局配置
        GlobalConfig globalConfig = new GlobalConfig();
        globalConfig.setOutputDir(System.getProperty("user.dir") + "\\tb-parent\\tp-bar\\src\\main\\java");//相对位置
        globalConfig.setOpen(false);  //生成完毕后是否打开代码所在目录
        globalConfig.setFileOverride(true); //是否覆盖原始生成的文件
        globalConfig.setIdType(IdType.ASSIGN_ID); //设置id生成策略
        autoGenerator.setGlobalConfig(globalConfig);
        //设置包名相关配置
        PackageConfig packageConfig = new PackageConfig();
        packageConfig.setParent("com.example");
        packageConfig.setEntity("pojo");
        autoGenerator.setPackageInfo(packageConfig);
        //策略设置
        StrategyConfig strategyConfig = new StrategyConfig();
        strategyConfig.setInclude("bars");  //对哪些表逆向生成
//        strategyConfig.setTablePrefix("tp_"); //生成的模块名去除这个前缀(因为生成的模块名默认带表名前缀)
        strategyConfig.setRestControllerStyle(true); //是否启用rest风格
        strategyConfig.setEntityLombokModel(true); //是否启用lombok
        autoGenerator.setStrategy(strategyConfig);
        //开始逆向生成
        autoGenerator.execute();

    }
}
